package org.tarena.dang.util;

import java.io.IOException;
import java.util.UUID;

/**
 * 邮箱验证码工具类
 * @author Administrator
 *
 */
public class VerifyCodeUtils {
	/**
	 * 生成UUID
	 * @return
	 */
	public static String generateCode() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString() ;
	}

	/**
	 * 获取邮箱验证码的UUID部分
	 * @param input
	 * @return
	 */
//	public static String getUUID(String input) {
//		if (input == null)
//			return null;
//		if (input.lastIndexOf("_") < 0)
//			return null;
//		return input.substring(0, input.lastIndexOf("_"));
//	}
	
	/**
	 * 获取用户ID
	 * @param input
	 * @return
	 */
	public static Integer getUserId(String input) {
		if (input == null)
			return null;
		if (input.lastIndexOf("_") < 0)
			return null;
		return Integer.valueOf(input.substring(input.lastIndexOf("_") + 1));
	}
	

	
	
	
	
	
	public static String getVerifyCode(Integer id){
		UUID uuid = UUID.randomUUID();
		String s = uuid.toString()+"-"+id;
		System.out.println(s);
		String es = EncryptUtil.Base64Encrypt(s.getBytes());
		return es;
	}
	
	public static String getUUID(String verifyCode){
		byte[] bys;
		try {
			bys = EncryptUtil.Base64Decrypt(verifyCode);
			String s = new String(bys);
			int index = s.lastIndexOf("-");
			return s.substring(0, index);
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}
		
		public static String getUserID(String verifyCode){
			byte[] bys;
			try {
				bys = EncryptUtil.Base64Decrypt(verifyCode);
				String s = new String(bys);
				int index = s.lastIndexOf("-")+1;
				return s.substring(index);
			} catch (IOException e) {
				e.printStackTrace();
				return "";
			}
	}
}
